package com.stationbuilding.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.stationbuilding.R;
import com.stationbuilding.fragment.MemberSelectFragment;
import com.stationbuilding.fragment.TreeSelectFragment;
import com.stationbuilding.fragment.document.DocumentFragment;
import com.stationbuilding.fragment.member.MemberFragment;

import java.lang.reflect.Member;

public class SelectActivity extends AppCompatActivity {

    private TreeSelectFragment treeSelectFragment;
    private MemberFragment memberFragment;
    private DocumentFragment documentFragment;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        // 初始化toolbar
        initToolbar();
        // 初始化列表数据
        initFragment();
    }

    private void initToolbar() {
        // 点击事件监听监听
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.back:
                        finish();
                        break;
                    case R.id.button:
                        if (id == R.id.liable_person || id == R.id.supervisor || id == R.id.acceptor || id == R.id.cc_person) {
                            memberFragment.setResult();
                        } else if (id == R.id.document) {
                            documentFragment.setResult();
                        } else {
                            treeSelectFragment.setResult();
                        }
                        break;
                }
            }
        };
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        ImageView back = (ImageView) toolbar.findViewById(R.id.back);
        Button button = (Button) toolbar.findViewById(R.id.button);
        TextView title = (TextView) toolbar.findViewById(R.id.title);
        button.setText(getResources().getString(R.string.sure));
        button.setVisibility(View.VISIBLE);
        back.setOnClickListener(onClickListener);
        button.setOnClickListener(onClickListener);
        Intent intent = getIntent();
        title.setText(intent.getStringExtra("toolbar_title"));
    }

    private void initFragment() {
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putString("ifdCode", intent.getStringExtra("ifdCode"));
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
        if (id == R.id.liable_person || id == R.id.supervisor || id == R.id.acceptor || id == R.id.cc_person) {
            memberFragment = new MemberFragment(true);
            memberFragment.setArguments(bundle);
            ft.replace(R.id.content, memberFragment);
        } else if (id == R.id.document) {
            documentFragment = new DocumentFragment();
            documentFragment.setArguments(bundle);
            ft.replace(R.id.content, documentFragment);
        } else {
            treeSelectFragment = new TreeSelectFragment();
            treeSelectFragment.setArguments(bundle);
            ft.replace(R.id.content, treeSelectFragment);
        }
        ft.commit();
    }
}
